package com.lzw.breezeNovel.webApp.service.impl.video;

import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.common.login.LoginUser;
import com.lzw.breezeNovel.common.login.LoginUserHolder;
import com.lzw.breezeNovel.common.result.ResultCodeEnum;
import com.lzw.breezeNovel.model.entity.pojo.user.UserInfo;
import com.lzw.breezeNovel.model.entity.pojo.video.ShortComment;
import com.lzw.breezeNovel.model.entity.query.PaginationResultQuery;
import com.lzw.breezeNovel.webApp.mapper.user.UserInfoMapper;
import com.lzw.breezeNovel.webApp.mapper.video.ShortCommentMapper;
import com.lzw.breezeNovel.webApp.service.video.ShortCommentService;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.video
 * @date 2025/3/23
 * @description 短视频的评论
 */
@Service
@SuppressWarnings("all")
public class ShortCommentServiceImpl implements ShortCommentService {

    /**
     * 注入mapping
     */
    @Autowired
    private ShortCommentMapper shortCommentMapper;
    /**
     * 注入用户mapper
     */
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 添加评论
     * @param content
     * @param shortId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(String content, Long shortId) {
        //获取当前用户
        Long userId = getCurrentUser();
        //创建评论对象
        ShortComment shortComment = new ShortComment();
        //设置评论内容
        shortComment.setContent(content);
        //设置评论用户
        shortComment.setUserId(userId);
        //设置评论视频
        shortComment.setShortId(shortId);
        //添加评论
        shortCommentMapper.insert(shortComment);
    }

    /**
     * 分页获取所以的评论
     * @param shortId
     * @param pageNo
     * @param size
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public PaginationResultQuery<ShortComment> list(Long shortId, Integer pageNo, Integer size) {
        //获取当前用户
        Long userId = getCurrentUser();
        //计算分页
        Integer start = getStart(pageNo, size);
        //获取评论总数
        Integer count = shortCommentMapper.count(shortId);
        //查询评论
        List<ShortComment> shortComments = shortCommentMapper.list(shortId, start, size, userId);
        //封装分页
        return new PaginationResultQuery<>(count, size, pageNo,shortComments);
    }

    /**
     * 计算分页
     */
    @NotNull
    @Contract(pure = true)
    private Integer getStart(Integer pageNo, Integer size) {
        //对分页进行判断
        pageNo = Optional.ofNullable(pageNo).orElse(1);
        size = Optional.ofNullable(size).orElse(10);
        pageNo = Math.max(pageNo, 1);
        size = Math.max(size, 1);
        //计算分页
       return (pageNo - 1) * size;
    }

    /**
     * 获取当前用户
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private Long getCurrentUser() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断是否登录
        if (loginUser == null) {
            throw  new BaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        //获取手机号
        String phone = loginUser.getPhone();
        UserInfo userInfo = userInfoMapper.selectByPhone(phone);
        return userInfo.getUserId();
    }
}
